import pandas as pd
import requests
import re
import time

data = pd.read_csv('world_all_country_history_data_2022_07_19.csv')
date_list = pd.DataFrame(pd.date_range('2021-01-01','2022-07-01'),columns=['date'])
country_list = data['country_name'].unique().tolist()
print(len(country_list))
#增加国家
data_add = pd.read_csv('缺失数据.csv')
data_add_list = data_add['country'].unique().tolist()
print(len(data_add_list))

#将两个数据整合
country_list.extend(data_add_list)
print(len(country_list))

def save_data(data,name):
    file_name = name+'_'+time.strftime('%Y_%m_%d',time.localtime(time.time()))+'.csv'
    data.to_csv(file_name,index=None,encoding='utf_8_sig')
    print(file_name+'保存成功')


for index, i in enumerate(country_list):
    date_list['country_name'] = i
    if index == 0 :
        date_lists = date_list
    else:
        date_lists = pd.concat([date_lists,date_list],axis=0)


date_lists['dict'] = date_lists['date'].apply(lambda x: x.strftime('%Y-%m-%d'))+date_lists['country_name']
data['dict'] = data['date']+data['country_name']

bq = dict(data[['dict','total_confirm']].values.tolist())
bt = dict(data[['dict','today_confirm']].values.tolist())
date_lists['total_confirm'] = date_lists['dict'].map(bq)
date_lists['total_confirm'] = date_lists['total_confirm'].fillna(method='ffill')
date_lists['today_confirm'] = date_lists['dict'].map(bt)
date_lists['today_confirm'] = date_lists['today_confirm'].fillna(method='ffill')

# save_data(date_lists,'xiugai')
print(date_lists[date_lists['date'] == '2022-03-08'])
# print(date_lists)